#!/usr/bin/python2

# input file: several rows where each row is  [ chrom, position ]
# return: the row with de corresponding base located on the bin chromosome
#
# to create the bin sequence:
# coords2sequence.py -bin chr??.fa

import sys, re, string, time, mmap

binPath = "/home/ciano/downloads/hgdownload.cse.ucsc.edu/goldenpath/hg19/chromosomes/"

chromFile=""
isopen=False
oldchrom=""
for line in open( sys.argv[1], "r" ):
    chrpos=line.strip().split("\t")
    if chrpos[0] == "chr0":
        continue
    if not chrpos[0] == oldchrom:
        seq = binPath + chrpos[0] + ".fa.bin"
        if isopen:
            chromFile.close()
        chromFile = open(seq, 'r+b')
        isopen=True
    mymap = mmap.mmap(chromFile.fileno(), 0)
    mymap.seek( int(chrpos[1])-1 )

    print chrpos[0] + "\t" + chrpos[1] + "\t" + mymap.read( 1 ).upper()
